/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import business.UserManager;
import entity.Message;
import entity.MessageType;
import entity.SentMessage;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 * It's the Bean managing the visualization of the user notifications in the
 * myMessages.xhtml page. During the construction it loads the messages and save
 * them in a list.
 *
 */
@ManagedBean
@ViewScoped
public class MessageBean {

    //The list of messages
    //List<Message> messageList = new ArrayList<>();
    //The list of tuples in SentMessage table: useful to know if messages are read or not.
    List<SentMessage> sentMessages = new ArrayList<>();

    @EJB
    UserManager um;

    @PostConstruct
    /**
     * initialize the lists of messages in the Bean.
     */
    public void init() {
        System.out.println("");
        //messageList = um.loadMessages();
        sentMessages = um.loadSentMessages();

    }
    /*
     public List<Message> getMessageList() {
     return messageList;
     }

     public void setMessageList(List<Message> messageList) {
     this.messageList = messageList;
     }
     */

    public List<SentMessage> getSentMessages() {
        return sentMessages;
    }

    public void setSentMessages(List<SentMessage> sentMessages) {
        this.sentMessages = sentMessages;
    }

    /**
     * The function passes to the page the real message's string to show to the
     * user
     *
     * @param message to show.
     * @return the string explaining the message.
     */
    public String printMessage(Message message) {
        switch (message.getType()) {
            case INVITED:
                return "You've been invited by " + message.getEvent().getCreator().getFirstName() + " " + message.getEvent().getCreator().getLastName()+".";

            case DELETED:
                return "The event has been deleted.";

            case MODIFIED:
                return "The event has been modified.";
            case ONE_DAY:
                return "Advice: there is a bad weather forecast for tomorrow.";
            case THREE_DAYS:
                if (message.getSugestedDate()!= null) {
                    Calendar suggestedDate = Calendar.getInstance();
                    suggestedDate.setTime(message.getSugestedDate());
                    return "Advice: there is a bad weather forecast for this event. We suggest you to pospone the event to "
                            + suggestedDate.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.US)+"/"
                            + suggestedDate.get(Calendar.DAY_OF_MONTH)+"/" 
                            + suggestedDate.get(Calendar.YEAR)+".";
                } else {
                    return "Advice: there is a bad weather forecast for this event. "
                            + "No sunny day available in the following week.";
                }
            default:
                return "Unsupported message type";
        }
    }

    /**
     * This function set the background color of the sentMessage according to
     * its status in the user message list. The message has two different colors
     * if it has been read or not by the user.
     *
     * @param smessage
     * @return the string representing the color
     */
    public String color(SentMessage smessage) {
        if (smessage.isPending()) {
            return "#b2ddff";
        } else {
            return "#ffffff";
        }
    }

    /**
     * The function that sets the pending field of the sentMessage to "read"
     * when the user clicks on the message.
     *
     * @param smessage the message to be set as read
     * @return the page redirecting
     */
    public String readMessage(SentMessage smessage) {
        System.out.println("Sono messageBean : leggendo messaggio " +smessage.toString());
        um.readMessage(smessage); 
        return "eventPage?faces-redirect=true&idEv="+smessage.getMessage1().getEvent().getId();
    }
}
